package com.bang.controller;

import com.bang.entity.Admin;
import com.bang.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/user/login")
    public String login(@RequestParam("aname") String username,
                        @RequestParam("password") String password,
                        Model model, HttpSession session){
        Admin admin = adminService.queryByNameAndPwd(username, password);
        if (admin!=null){
            session.setAttribute("loginUser",username);
            return "main";
        }else {
            model.addAttribute("msg","用户名或密码错误！");
        }
        return "index";
    }

    @RequestMapping("/user/logout")
    public String login(HttpSession session){
        session.invalidate();
        return "redirect:/index.html";
    }
}
